package com.flowerfling;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class FlingCocosBridge {

    private static FlingCocosBridge flingCocosBridge;
    public static WeakReference<Activity> mainGameActivity;
    private Method evalString;
    private Method runOnGLThread;
    public static Context context;

    public static FlingCocosBridge getInstance() {
        if (flingCocosBridge == null) {
            flingCocosBridge = new FlingCocosBridge();
        }
        return flingCocosBridge;
    }

    public void init(Context ctx) {
        try {
            context = ctx.getApplicationContext();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setMainGameActivity(Activity activity) {
        mainGameActivity = new WeakReference<>(activity);
    }

    private static final Handler handler = new Handler(Looper.getMainLooper());

    public void ADVideoCallback(String action, String jsonData) {
        if (mainGameActivity == null || mainGameActivity.get() == null || context == null) {
            return;
        }
        handler.post(() -> {
            try {
                if (runOnGLThread == null) {
                    getRunOnGLThread();
                }

                if (evalString == null) {
                    Class clzBridge = Class.forName(FlingConstant.COCOS2DX_JJB_PATH, false, context.getClassLoader());
                    evalString = clzBridge.getDeclaredMethod("evalString", String.class);
                    evalString.setAccessible(true);
                }

                runOnGLThread.invoke(mainGameActivity.get(), (Runnable) () -> {
                    callback(action, jsonData);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }


        });
    }

    private void getRunOnGLThread() {
        try {
            Class clzCocos2dAct = Class.forName(FlingConstant.COCOS2DX_ACTIVITY_PATH, false, context.getClassLoader());
            runOnGLThread = clzCocos2dAct.getDeclaredMethod("runOnGLThread", Runnable.class);
            runOnGLThread.setAccessible(true);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void callback(String action, String jsonData) {
        try {
            evalString.invoke(null, String.format("window.AdVideoCallback('%s','%s')", action, jsonData));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showToast(final String message) {
        if (mainGameActivity == null || mainGameActivity.get() == null) {
            return;
        }
        mainGameActivity.get().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(mainGameActivity.get(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
